Rectangular partition for n-dimensional images with arbitrarily shaped rectilinear objects

Partitioning two- or multidimensional polygons into rectangular and rectilinear components is a fundamental problem in computational geometry. Rectangular and rectilinear decomposition have multiple applications in various fields of arts as well as sciences, especially when dissecting information into smaller chunks for efficient analysis, manipulation, identification, storage, and retrieval is essential. This article presents three simple yet elegant solutions for splitting geometric shapes (particularly non-diagonal ones) into non-overlapping and rectangular sub-objects. Experimental results suggest that each proposed method can successfully divide n-dimensional rectilinear shapes, including those with holes, into rectangular components containing no background elements. The proposed methods underwent testing on a dataset of 13 binary images, each with 1 … 4 dimensions, and the most extensive image contained 4096 elements. The test session consisted of 5 runs where starting points for decomposition were randomized where applicable. In the worst case, two of the three methods could complete the task in under 40 ms, while this value for the third method was around 11 s. The success rate for all the algorithms was 100 %.

For metrology and building design, rectangular partitioning can be suitable for splitting an object or a part thereof for more accurate measurements.Applications in data compression, dimensionality reduction, and component analysis could involve decomposing a complex shape or object into rectangles or cuboids and saving only their coordinates and dimensions instead of recording the whole geometry.In computer vision, rectangular partition techniques can be used for feature extraction and as part of other algorithms, such as skeletonization or thinning.Höschl and Flusser [6] mention rectangular decomposition as a tool to speed up feature calculation for describing and recognizing 3D shapes.In game design and simulations, rectangular partition or decomposition has potential applications in, for example, level design, artificial intelligence (navigation and pathfinding), or physics modeling.Four-dimensional rectangular partition could be used to study changes in buildings or other environments over time, or it might be utilized in games or simulations featuring interlinked 3D worlds.E-mail address: ville.pitkakangas@centria.fi.Possible results of rectangular partition: even a simple L-shaped 2D object (first shape from the left) can be decomposed in at least three unique ways (second, third, and fourth shape from the left).Dashed lines denote sub-object edges. V

Overview
In this article, three solutions for rectangular partition are presented.The first is a solution for cases where the entire image or volume must be iterated.The second and third solutions are more generalizable; they offer random access to the image data and the option to process only part(s) of the image.The proposed methods are named Special, General I, and General II.
The proposed methods use the highest common neighborhood denominator when determining which parts belong in which connected component: lines for 2D, faces for 3D objects, et cetera.The idea is like the concept of hypercubic meshes in Ref. [14].This type of neighborhood connection is used to ensure there are no holes in the extracted shapes.The output of each solution can be presented as an image of the same shape and size as the input, with a unique label assigned to each extracted rectangular and rectilinear component.
In essence, when using Special and General I methods, the process of rectangular partition consists of three steps: finding the starting point; expanding the selection until background, visited foreground elements, or array edges are reached; and finding holes in the selection and shrinking it until there are no more holes in the region.Without the last step, the extraction result would resemble a convex hull, covering all holes and possibly even dents of the object.The General II method differs from the other two because it relies on template matching.
The proposed techniques have a few limitations.The input must be provided as bitmaps, rather than n-dimensional meshes or vector graphics.The solutions are designed for dimensionally optimized input arrays, meaning all dimensions must have a length greater than one.In addition, while extracted shapes are rectangular, the outcome may not be optimal or fit for a particular purpose.In other words, even though the output only has rectangular objects, the objective of the partitioning process is not to explicitly find or use the smallest, the largest, the most, or the fewest shapes.However, it was still considered beneficial for potential applications that the partitioned shapes have more than one unit of length in all dimensions whenever possible.An optimization algorithm was, therefore, developed to minimize the number of extracted objects.Since the generalizable solutions rely on randomness, the results may vary on each execution.Shapes inside or overlapping bounding boxes of other shapes may impose another limitation due to the V. Pitkäkangas Fig. 2. Dataset.Each array has its name written above it.The "Hypershape" array is four-dimensional but split into three-dimensional slices along the w-axis for visualization."Squares" and "Cross 2D" are two-dimensional, and "Lines" is one-dimensional.All the other arrays are threedimensional.The purple color in 1D and 2D shapes represents the background.([[[1,2], [3,4]],[ [5,6], [7,8] [ [3,4,4,3], [5, [5,25,30], [10,2,1]]) b = list(np.ndindex(a.shape))b is [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)] product cartesian product from the itertools Python package; the "repeat" argument can be used to define the number of repetitions for each element possibility of these separate but close objects being mistakenly recognized as parts of each other.This problem can be solved by applying connected-component labeling on the image before attempting to partition it, copying each labeled object to a blank image, and performing the rectangular partition on each new picture, thus processing each object separately.
The coordinate system used is generally that of the array (image), meaning almost no transforms to local coordinates of the shapes or regions are done, thus resulting in the extracted shapes being always axis-aligned and never tilted according to the orientations of the source objects.
All solutions were implemented in the Python programming language [15] with ease of implementation, high precision and reliability, and dependency on as few software libraries as possible in mind.The only software package the solution implementations depend on is [16], which is used for array operations.
Matplotlib [17] and Pillow [18] were used for visualizing the datasets and test results for this article.Three-dimensional arrays and slices are shown from two sides; this was realized by saving the view, recording its azimuth and elevation angles, adding 180 • to the former and multiplying the latter with − 1, redrawing the view using the new angle parameters, and saving the second view.This technique was used for Figs. 2, 8 and 9.
Algorithms for the proposed methods are presented in Python-like pseudocode in Sections 2.3 and 2.4.For readers unfamiliar with the Python language, a glossary of some terms and symbols used in the algorithms is shown in Table 1.In arrays and other iterable variables, indexing starts from zero.
The mathematical justification for all three decomposition methods lies in array decomposition, block identification, and efficient Fig. 3. Test process.The same optimization procedure is used regardless of the selected partition method.
storage.To elaborate, by decomposing the input array into uniquely labeled contiguous blocks containing only foreground elements, the solutions allow for efficient array manipulation and analysis because the contents are broken down into smaller, more manageable blocks that are constructed around foreground elements and can be identified and represented by their position and dimensions.This provides for easy access and manipulation of image data.Put differently, by performing this decomposition, the methods facilitate efficient storage and retrieval of information, theoretically simplifying and speeding up data analysis.

Test procedure
The dataset the developed methods were made with and tested on consists of 13 synthetic n-dimensional binary images, where n ∈ {1, 2, 3, 4} (Fig. 2).In each test image, zero-value elements represent the background, and all the other elements belong to the foreground, thus being parts of objects.Although most use cases of rectangular partition use 2D and 3D data, a few examples of 1D and 4D arrays exist in the set to test and demonstrate the capabilities of the developed algorithms.The "Randomly Generated" shape in Fig. 2 was generated randomly, and all the other arrays were created manually."Squares" and "Cubes" test the capability of the algorithms to handle data with multiple objects.All the arrays with "Holed" in their names are used to see if shape orientation affects partition results.
The tests were done on a Dell Precision 7520 laptop.
The test procedure (Fig. 3) is as follows: each array in the input data (Fig. 2) is given individually to each partition method.The starting point is picked randomly from all object elements for both General solutions; after a rectangular shape has been successfully extracted, a new point is chosen randomly until there are no more unvisited object elements.The results of both Special and General solutions are optimized so that extracted objects are merged whenever possible, and their assigned labels are recalculated so there are no skipped values.Each input array is partitioned five times using each method.This is to investigate the effect of randomness in object participation and to improve the testing process thoroughness.The execution times of testing and its phases are measured, and key performance indicators used for evaluating the methods and their effectiveness are calculated from the results (Section 3).The partitioning is considered successful if the extracted shape only consists of foreground elements of the same label, unique to this subobject.

Special Solution
Arguably, the simplest way to implement rectangular partitioning for n-dimensional images containing only rectilinear shapes that may or may not have holes is to iterate through the image sequentially in one pass.The advantages of this method include ease of implementation, predictability of results, and repeatability of the process.The most obvious disadvantage is the lack of random access; the entire array must be traversed even when analyzing only a part of the image would be desirable.
The procedure for the Special solution for rectangular partition is presented in Algorithm 1.It takes one argumentthe image (array) that will be processedand returns an array containing rectangular subobjects with unique labels and a list of starting-point coordinates for each subobject.The output and input arrays are of equal shapes and sizes.
In short, the Special method works as follows: The initial step is discovering the first unvisited object element.After that, its position is used as the starting point for the new sub-object.The algorithm moves along every axis from this location until it hits the array boundary, a visited object element, or the background.The coordinates found this way are used as region edges and potential endpoints of the sub-object.The last step is hole detection, which is realized simply by finding the lowest coordinates of background elements within the region and cropping such elements out of the sub-object if they are found; otherwise, the original region start point and end points are used.The process starts from discovering the first unvisited object element until there are no more such objects.
The formulae at the basis of Algorithm 1 focus on finding the coordinates of zero (background) and non-zero (foreground) elements, block slicing, and block size calculation.We can view the method presented in the algorithm as an application of set theory, linear algebra, and graph theory: The decomposition procedure constructs element sets from matrices (images) by using specific conditions to find coordinates.Linear algebra concepts, particularly matrix operations including element-wise comparison and slicing, are applied to represent and manipulate n-dimensional arrays (images).Additionally, the method utilizes graph traversal and iterative graph exploration to identify the connected components (extracted foreground subregions) within the imagethe foreground elements serve as graph nodes.

General Solution I
This method can access array data randomly, meaning images need not be processed fully, and their shapes or objects can be partitioned in arbitrary order.
The sub-object extraction algorithm that forms the most crucial part of the solution is described as pseudocode in Algorithm 2. It is based on finding an enclosing (n-dimensional equivalent of) cuboid for the chosen object element and shrinking the enclosing cuboid until it no longer contains background elements.To simplify the process, the algorithm works on a copy of the array where it assigns an otherwise unused label to the target element (or "marks the target pixel with a special color") and uses the "where" function of [16] to locate it every time the geometry of the enclosing cuboid changes.This minimizes the need for coordinate transforms.
Mathematically, Algorithm 2 involves an iterative refinement process and n-cuboid boundary adjustment to extract accurately a sub-shape from an image.The formulae used in the algorithm primarily focus on bounds adjustment such that the method obtains the region of interest with iterative boundary search (partly done in Algorithm 3) and a foreground cross-out operation (Algorithm 4).The shape and content of this extracted area contribute to determining the n-cuboid bounds while also involving setting new slices to refine said boundaries.The background of the algorithm lies in array manipulation, iterative optimization, set theory, and computational geometry.The procedure manipulates image indices and slices to extract and refine regions of interest and n-cuboid boundaries, leveraging concepts including subsets, minimum and maximum values, and Euclidean distances in locating background and foreground elements.The goal is to tightly enclose the extracted n-cuboidal sub-object encompassing the target element while excluding background elements, thus enhancing the accuracy and completeness of the partitioning.
The initial step is saving the location of the original target element, finding the enclosing cuboid (using Algorithm 3) in an uncropped array, cropping the image to the resulting coordinates, and storing the new shape to a variable.
After completing the above step, the algorithm checks if there are background elements in the cropped region.If this condition is met, the position information of the target element is updated to reflect the new coordinates.The cropped area is copied, and a cleanup operation (Algorithm 4) is performed on the copy to eliminate holes, concavities, angles, and intersections.
Next, the element located in the copy in the coordinates corresponding to the target position is checked.If the object in it was erased in the cleanup (i.e., is now zero), an element-wise subtraction is performed between the copy and the original cropped region with the former as the minuend and the latter as the subtrahend.The negative is taken from the result and used as the image array in subsequent steps, starting from finding new bounds (Algorithm 3) and cropping the region accordingly.
If the object in the target position in the copy was not erased, the copied array (post-cleanup) is used as the new image array in the remaining steps.New bounds are discovered (Algorithm 3), and the region is cropped to reflect these.
If the cleanup did not help crop the region (its shape did not change), another procedure is attempted: the cropped area is split into 2 n sub-regions, where n is the number of dimensions in the array.The position of the target element is used as the pivot and is included in every sub-region.For each split array, the minimum and maximum coordinates of background elements are saved to MNS and MXS, respectively, and used as a foundation of two new lists: one with minima of maxima and the other with maxima of minima (Equation ( 5) and Equation ( 6)): and where m is the number of dimensions in the cropped region.These two lists now consist of per-axis minimum and maximum background element coordinates compiled from each sub-region and are combined into a single list of lists where minima are the first and maxima are the second elements.This list, whose length is equal to the number of dimensions in the cropped region, is iterated through, and target element coordinates and a constant value of one are added to the maxima.Three values are checked on each iteration: minimum, target element coordinate, and the adjusted maximum.The smallest of these is chosen as the start and the largest as the end for a slice of the current axis.A tuple is formed from the slices created when processing the list and used to clip the cropped region further.
If splitting the cropped region and using a combination of minimum and maximum background coordinates could not reduce the area, the position of the target element is compared against the largest coordinate in the cropped region (for example, the bottom right corner in 2D arrays).If the two are equal, the distances between this point and all the background elements are computed by summing the absolute values of the per-axis coordinate differences and taking the square roots of the results.The background element with the longest distance to the target point is used as a reference (the shortest distance would often result in too small areas): the region is further cropped by using its coordinates as the starting point, while the target element serves as the endpointbut only if all V. Pitkäkangas dimensions of the resulting area are one or higher.This step is meant to crop the region so that edges that only consist of object elements ideally get cut out, and the resulting clipped array can be processed further in the next iteration.
The final recourse (when the region still has the same size as in the second step or the previous process would have caused it to have zero in its dimensions) is to pick the coordinates of the target element and nothing else, that is, a rectangular sub-object one unit long in every dimension.
The variable containing the shape stored in the first step is updated with that of the cropped region.
The steps above, starting from checking the presence of background elements in the region of interest, are repeated until this condition is not met.
Once the sub-object has been extracted, its coordinates are transformed back by subtracting the location of the new target (in the sub-shape) from the old position (in the original array) and constructing a tuple of slice objects with the coordinate differences as the starts and the sum of them and sub-object dimensions as the ends.This tuple now contains the global coordinates of the extraction result.
The code in Algorithm 2 is executed with randomly chosen foreground elements as the target pixels until the whole array is processed.Each time an enclosing cuboid is obtained, the region of the array corresponding to its coordinates is set to the background.This step reduces the possible foreground (or target) elements on every iteration.
The boundary-finding procedure (Algorithm 3) traverses the array in both directions along every dimension until it hits an array bound or a background element.This is like the procedure in the Special solution except bidirectional and does not consider visited elements.
The mathematical justification of Algorithm 3 comprises n-cuboid bound identification and iterative search along each dimension.The first is essential for isolating sub-objects within the image, and the second ensures the n-cuboid bounds the foreground elements in all directions.The formulae behind the algorithm include coordinate update and n-cuboid representation.In this context, the former adapts the minimum and maximum coordinates of the n-cuboid by foreground element positions, whereas the latter builds a structured representation of the n-cuboid.Algorithm 3 uses coordinate manipulation (processing array indices to access specific image elements and ensuring the accuracy of the n-cuboid bounds) and iterative search strategy (iterating over each dimension to search for foreground and background elements around the target element, guaranteeing the completeness and efficiency of the bounding box calculation).In summary, the algorithm employs concepts from linear algebra, iterative search strategies, and geometry.
The cleanup operation (Algorithm 4) works as follows: the locations of background elements in the array are gathered, and for each such element, n regions of interest are formed, with n being the number of dimensions in the array.Each region spans across the entire array along one axis but is only one dimension long along the rest.The array parts corresponding to the formed areas are set to background elements.An example using the "Holed" image (with the surrounding background already cropped out) from the test dataset is given in Fig. 4.
Algorithm 4 has a mathematical background in set theory and matrix manipulation techniques, specifically in-place modification: the function efficiently uses array indices and slices to identify and modify background-associated elements.The manipulation operations ensure these elements do not affect subsequent partition steps, which is crucial for accurate decomposition and isolating relevant regions of interest (foreground sub-objects).A critical step in the process is slicing for cross-out, where the algorithm constructs slices to cover entire axes containing background elements, efficiently identifying and modifying foreground elements associated by axis with each background element.Although creating a copy of the image in Algorithm 2 for Algorithm 4 to work on requires additional memory, in-place manipulation still reduces mathematical complexity and enhances efficiency, especially for large images.In short, the justification for Algorithm 4 lies in its efficiency in modifying elements associated with the background so that even shapes with concave angles can be decomposed accurately.

General Solution II
Like General Solution I, this method can access data randomly.However, the most vital difference to the other solution is that this second procedure uses template-matching to find the sub-object boundaries.A template here is a rectangular shape with the same number of dimensions as the array.Each element in the region covered by the template must be part of an object, and the largest template area enclosing no background elements is chosen as the region of the extracted sub-shape.
The templates are formed by calculating all possible permutations of shapes up to the dimension set based on the maximum number of consecutive foreground elements found on each axis.For example, for a 2D array where this value is two on both axes, the largest template size would be 2*2 elements, and the outcomeexcluding templates with zero in their shapewould be four permutations: (1, V. Pitkäkangas 1), (1, 2), (2, 1), and (2, 2).A pseudocode representation of this calculation is in Algorithm 6.The product function from the itertools software package that is part of the standard Python distribution is used to get all permutations.
The upper bound for the template sizes is calculated as follows: as a pre-processing step, the array is essentially sliced along every axis into pieces with the length of the axis as one dimension and zeros as others.For a 3D array having 3*3*3 elements, this would produce slices of 3*1*1, 1*3*1, and 1*1*3 units, and there would be 9 (3*3) slices of each type.However, the slices are formed as 1D arrays, so the example input would produce nine 3-unit-long slices along each axis, so 27 arrays in total.The slicing process is in pseudocode in Algorithm 9, and the code for calculating dimension and shape permutations for it is in Algorithm 7.Each slice returned by the process has its axis ID attached.Once the slices have been formed, the template boundary calculation can resume.A list l of the maximum number of consecutive foreground elements on every axis is created, and each value is initialized to zero.Next follows the processing of the slices.The axis ID is read, and the positions of elements of the background elements are discovered.If the slice only has foreground elements, its maximum number is set to its length.If there is one background element, the maximum number of the slice is set to one.Otherwise, the maximum of the absolute values of differences between the foreground element positions is used.This would be three for a slice a = [0,1,0,1,1,1,0].The maximum value of the slice is compared to that saved to the index corresponding to the axis ID in the list l created before processing the slices.If this maximum exceeds the stored value, l is updated to hold the new value instead.After processing all the slices, the function returns a list containing the lengthiest sequence of foreground elements for each axis.A pseudocode representation of this procedure is in Algorithm 8.
A simple example of permutations is provided in Fig. 5, where a 4*4-element 2D image with an object of 2*2 elements (gray squares) is surrounded by the background (white squares).The bottom-right part of the object has been chosen for the starting point; this is highlighted in darker gray than the rest of the shape.The dotted lines show the possible locations of the 2*2 template when the starting point is included in the region of interest.The leftmost part of Fig. 5 displays all possibilities at once; the result looks like a 3*3 grid.The other four parts show each template position separately.The second part from the left is the ideal solution because the area covered by the template only contains elements belonging to the object.
Once the maximum number of consecutive foreground elements on each axis has been computed, the output has been used to set the upper bounds for template creation, and the templates have been created, the sub-object extraction begins.For each template, possible starting positions are computed so that the target position is included, and secondly, there are no reads out of the array.The first criterion reduces processing time by avoiding unnecessary analysis of regions that may not be part of the currently targeted (sub-) object.The template is fitted for each remaining starting position: a sub-array is produced by using the coordinates of the location and template dimensions, and if all elements in this sub-array belong in the foreground, the sub-array size is compared to that of the best fit so far.If the size is larger, the best fit is updated to reflect this: it contains the new size, the coordinates and dimensions of the sub-array, and the template shape.Once the list of templates has been exhausted, the best fit is returned (Algorithm 5.).
The core of the partition process is described in Algorithm 5. Like the principal part of General Solution I, the code in Algorithm 5 is executed with randomly chosen foreground elements as the target pixels until the whole array is processed.Each time a sub-shape has been extracted, the region of the array corresponding to its coordinates is set to the background.This reduces the possible foreground (or target) elements on every iteration.
From a mathematical perspective, the rationale behind Algorithm 5 lies in contiguous block identification to detect the most sizable such region (a foreground object or sub-object).The method employs a systematic search to achieve this goal by iterating over possible dimensions and starting indices, using formulae that manage block size computation (based on array analysis, matrix manipulations, and set theory) to check if the block contains only foreground elements.The iteration techniques ensure computational efficiency, especially for large images: using permutations (Algorithms 6 and 7) and the lengthiest per-axis non-zero (foreground) element sequences (Algorithm 8) as references when computing the n-cuboid sizes and positions and using the fast n-dimensional index operator implemented in NumPy.Additionally, Algorithm 5 uses Algorithm 9 and mathematical concepts related to it in some of its steps.
The mathematical background of Algorithms 6 and 7 is primarily in combinatorial analysis and related techniques and secondarily in matrix operations (namely, obtaining dimensionality and creating slices for the permutations).Using cartesian products of the inputs, the objective of the algorithms is to systematically generate comprehensive and unique permutations that explore different array shapes, structures, and element subsets along various dimensions.The methods ensure all possible combinations, either of array dimensions within specified limits (Algorithm 6) or slices along dimensions (Algorithm 7), are explored.The algorithm outputs later find application in array (image) subset analysis and manipulation.
Algorithm 8 relies on algorithmic techniques, numerical analysis, set theory, and linear algebra (matrix operations) to identify sequences of consecutive non-zero (foreground) elements along each axis of the array (image) and to compute the maximum length of V. Pitkäkangas the resulting lines through axis-wise systematic analysis and mathematical techniques.These methods make image slice computations used in the sequence identification task efficient and accurate.In addition to matrix operations and set theory, finding runs of foreground elements and computing differences between their indices are utilized.The result provides information about the spatial distribution of the foreground elements within the image.
Algorithm 9 is based mathematically on combinatorial analysis, which generates all possible dimension options and slice permutations, and an iterative approach to explore possible choices and rapidly generate comprehensive combinations for slicing along each dimension.Enumerating these slice permutations ensures a thorough slicing combination coverage, making the exploration of array element subsets along different dimensions possible.The objective is to facilitate the array subset analysis and manipulation necessary for polygon partitioning in General Solution II.Algorithm 5. "General" solution II for rectangular partition.Algorithm 6. Auxiliary method "array_permutations".

Optimization
Partition results can be optimized by merging adjacent rectangular shapes.This, however, can only be done if two conditions are met: Firstly, the shapes must share their centroid coordinates in all dimensions except one, and secondly, their boundaries corresponding to shared coordinates must have equal length.The merging process is repeated until no more shapes that meet the conditions are found.A non-exhaustive list of examples of shapes that can and cannot be merged is shown in Fig. 6: three pairs of 2D shapes and three pairs of 3D shapes.
Results can be further cleaned after optimization by reassigning unused labels of extracted shapes.For example, if the optimized array only has labels 0 (background), 1, 3, and 4, these can be reassigned to 0, 1, 2, and 3.
The optimization process is presented in pseudocode in Algorithm 10, and the evaluation of the partition, optimization, and label reduction procedures is described similarly in Algorithm 11.
Mathematically, the justification of Algorithm 10 is in processes that guarantee precise label comparison and reduction.It involves comparing the centroids of labeled sub-objects within their bounding boxes and the dimensions of these boxes to confirm a distinct separation that allows for merging only when regions are adjacent and proportionate in related dimensions.Additionally, it can minimize label values without compromising the integrity of the optimization.The foundation of Algorithm 10 exists in calculating and comparing centroids, determining bounding box sizes, and optionally reducing labels.Its mathematical framework includes analyzing centroids and bounding box dimensions to evaluate the merging suitability of labeled subregions.The analysis ensures spatial coherence and remaps labels to maintain segmentation accuracy while preserving the spatial relationships between regions.This dual approach of centroid analysis and label remapping ensures both the accuracy of segmentation and the efficiency of label use.
A critical aspect of Algorithm 10 is its dependency on an evaluation function (Algorithm 11) to check that the optimization process has not introduced errors or inaccuracies.The mathematical justification and background of the evaluation (Algorithm 11) consist of region contiguity and validity realized through iterative region analysis for studying each labeled region and ensuring comprehensive coverage of all areas, the combination of array operations and set theory for efficient region extraction and manipulation while also ensuring computational efficiency, and Boolean logic to determine the success status of the decomposition evaluation, indicating clearly whether the outcome is valid.Therefore, the function verifies that the regions have neither gaps nor disjoint components and ensures that each labeled region contains only one unique non-zero label value, indicating that all regions solely consist of foreground elements and that the optimized image contains no mixed nor overlapping sub-objects.

Experimental results
All three solutions processed the test dataset successfully, with neither overlapping shapes nor background elements in any of the extracted rectangular shapes.
Six metrics were chosen to evaluate the methods, and three operations were applied to each, resulting in 18 key performance indicators.The operationsminimum, maximum, andwere performed for each array and method by finding the smallest and largest values and computing the arithmetic mean from the corresponding metrics (every array is processed five times with all methods).For example, the first minimum time for General II is 0.321 s.Out of the five test runs, this is the shortest processing time of Fig. 6.Conditions for merging adjacent shapes.
V. Pitkäkangas the method for the first array (named "Box 9 × 9 × 9").In addition to execution time, the number of labels in the results, region size, and reduction count are used.The first two of these are calculated before and after optimization.Region size is determined solely by the number of elements in the extracted shape.Reduction count is the set difference between the labels in optimized and unoptimized results.
In addition to the 18 per-array and per-method metrics above, some indicators for the entire dataset were computed and used in the evaluation (Equations ( 7)-( 9)).These are per-method averages for times, optimizations for shape sizes and labels, and reductions for every method.The first of these is simply the arithmetic mean of execution times.Average per-method optimization for shape sizes o s is calculated by using o s = 100 * s − 100, (7) where s denotes the arithmetic mean of size ratios for the respective method (Special, General I, or General II).Conversely, average per- The first General solution performs fastest on experimental data, but its results are the least predictable.The second General solution is a tradeoff between speed, accuracy, and predictability of results.Despite its shortcomings, particularly in execution time, this method is still expected to perform reasonably effectively in the future due to advances in computational power.Some other criteria for General solutions in place of random selection might prove more effective solutions with more predictable results for partitioning shapes into rectangular subcomponents.One potential alternative is basing the choice of the next unvisited "target" element on the distance to the last extracted sub-object and values of neighbors of possible "target" elements.These criteria could be a basis for solutions that enable more optimal or application-appropriate sub-object extraction.Furthermore, the General methods might be sped up  V. Pitkäkangas

Fig. 1.
Fig. 1.Possible results of rectangular partition: even a simple L-shaped 2D object (first shape from the left) can be decomposed in at least three unique ways (second, third, and fourth shape from the left).Dashed lines denote sub-object edges.

Fig. 4 .Algorithm 3 .Algorithm 4 .
Fig. 4.An example of the non-zero cutout process.From left to right: Input, cutout regions computed from background elements (said elements excluded from the picture for clarity), cutout regions superimposed on input, output with cutout regions set to background elements.

Fig. 5 .
Fig. 5. Example of permutations for a template size of 2x2 elements and on a 2D image with a target element.The leftmost array has all possible permutations stacked, while each possible permutation is presented individually in the others.

Algorithm 10 .Algorithm 11 .
Optimization procedure of partitioned shapes.V. Pitkäkangas Evaluation of partition and optimization results.

Table 1
Glossary of Python terms and symbols used in pseudocode representation of the proposed algorithms.

Table 2
Test results for each array in the dataset.